package com.mobilekites.api.model;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.Response.Listener;
import com.mobilekites.api.DfeApi;
import com.mobilekites.entity.AppConfig;
import com.mobilekites.util.KitesLog;

public class DfeAppConfig extends DfeModel implements Listener<JSONObject> {
	private JSONObject mLastResponse;
	private DfeApi mDfeApi;
	private AppConfig mAppConfig;

	public DfeAppConfig(DfeApi dfeApi) {
		mDfeApi = dfeApi;
	}

	@Override
	public void onResponse(JSONObject response) {
		getItemsFromResponse(response);
		mLastResponse = response;
	}

	public AppConfig getItemsFromResponse(JSONObject response) {
		KitesLog.d("Config response=" + response.toString());
		JSONObject jsonObject;
		try {
			mAppConfig = new AppConfig();
			jsonObject = response.getJSONObject("data");
			mAppConfig.about = jsonObject.getString("about");
			mAppConfig.isShowMoreApp = jsonObject.getBoolean("isShowMoreApp");
			mAppConfig.admob = jsonObject.getString("admob");
			mAppConfig.urlMore = jsonObject.getString("more_app");
			notifyDataSetChanged();
			return null;
		} catch (JSONException e) {
			e.printStackTrace();
			KitesLog.e("Get app config error, parse json");
		}
		return null;
	}

	public AppConfig getAppConfig() {
		return mAppConfig;
	}

	@Override
	public boolean isReady() {
		return mLastResponse != null;
	}

	private Request<JSONObject> makeRequest() {
		return mDfeApi.getConfig(this, this);
	}

	public void startLoadData() {
		makeRequest();
	}
}
